
// Definition for Employee.
#include <unordered_map>
#include <vector>

class Employee {
public:
    int id;
    int importance;
    std::vector<int> subIds;
};

class Solution {
private:
    std::unordered_map<int, Employee*> empMap;
    int res = 0;
public:
    int getImportance(std::vector<Employee*> employees, int id) {
        for (Employee* emp : employees) {
            empMap[emp->id] = emp;
        }

        return postOrder(id);
    }

    void preOrder(int id) {
        Employee* emp = empMap[id];
        if (!emp) return;
        res += emp->importance;
        for (int child : emp->subIds) {
            preOrder(child);
        }
    }

    // 递归 - 后序遍历
    // 计算 id 重要度之和
    int postOrder(int id) {
        Employee* emp = empMap[id];
        if (!emp) return 0;

        // 多次递归调用
        int total = 0;
        for (int child : emp->subIds) {
            total += postOrder(child);
        }

        return emp->importance + total;
    }

};